﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;

namespace WpfUMLTry1.Data
{
    class GameDataManager
    {
        private static String directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

        public static Model.gameModel LoadGame(Int32 slotNum)
        {
            Model.PlayerCharacter ply;
            String fName = "saveG" + slotNum + ".rpg";
            StreamReader reader = new StreamReader(fName);

            LoadCharacter(reader, out ply);
            if (ply == null)
            {
                File.Delete(fName);
                throw new FileIOExc(slotNum);
            }
            Int32 storyState = Int32.Parse(reader.ReadLine());
            reader.Close();

            Model.gameModel gModel = new Model.gameModel(slotNum, storyState);
            gModel.addCharacterToTheGame(ply);
            gModel.numberOfSaveSlot = slotNum;
            return gModel;
        }

        public static void SaveGame(Model.gameModel gModel) 
        {
            String fName = "saveG" + gModel.numberOfSaveSlot.ToString() + ".rpg";
            StreamWriter writer = new StreamWriter(fName);
            SaveCharacter(gModel.GetPlayCaharacter(), writer);
            writer.WriteLine(gModel.storyState);
            writer.Close();
        }

        public static void SaveCharacter(Model.AbstractCreature chr, StreamWriter writer) 
        {
            writer.Write(chr.getBackSavingText());
        }

        public static void LoadCharacter(StreamReader reader, out Model.PlayerCharacter ply) 
        {
            String job = reader.ReadLine();
            String[] data = reader.ReadLine().Split('|');
            if (verifyData(data))
            {
                if (job == "Warrior")
                    ply = new Model.Classes.Warrior(data, data[0]);
                else if (job == "Mage")
                    ply = new Model.Classes.Mage(data, data[0]);
                else if (job == "Rogue")
                    ply = new Model.Classes.Rogue(data, data[0]);
                else
                {
                    ply = null;
                }
                return;
            }
            ply = null;
        }

        public static Boolean fileExsists(String fName)
        {
            String filePath = Path.Combine(directory, fName);
            return File.Exists(filePath);
        }

        private static Boolean verifyData(String[] data)
        {
            Boolean valid = true;
            int output;
            for (int i = 1; i <= 4 && valid; ++i)
                valid = valid && Int32.TryParse(data[i],out output);
            for (int i = 6; i <= 10 && valid; ++i)
                valid = valid && Int32.TryParse(data[i], out output);
            valid = valid && (data[5] == "RAGE" || data[5] == "MANA" || data[5] == "ENERGY");
            return valid;
        }
    }
}
